Unique Paths

Try to solve the Unique Paths problem.

Statement#

Imagine a scenario where an adventurous little robot, named Robi, has a mission to traverse a grid with m rows and n columns and reach a treasure box placed at grid[m−1][n−1]grid[m-1][n-1]. Robi starts the journey from its home in the top-left corner of the grid, grid[0][0]grid[0][0]. However, it faces a constraint that at any given time, it can only move in either of the two directions: downward or to the right.

Given the two integers, m and n, return the total number of unique paths that Robi can take from the grid's top-left corner to reach the bottom-right corner of the grid.

Constraints:

  • ≤\leq mn â‰¤\leq 100

Example#

svg viewer

1 of 4

svg viewer

2 of 4

svg viewer

3 of 4

svg viewer

4 of 4

Understand the problem#

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Unique Paths

3

What will be the output if the following values of input are given?

mm = 3

nn = 4

Your Answer
A)

4

Correct Answer
B)

10

Explanation

There are a total of ten unique paths from the top-left corner to the bottom-right corner of the grid.

C)

12

D)

14

Question 3 of 33 attempted

Try it yourself#

Implement your solution in the following coding playground:

The optimal solution to this problem runs in O(m x n) time and takes O(m x n) space.

Python
usercode > main.py
Powered by AI
Input #1
Input #2
Unique Paths

You might want to go over the Dynamic Programming pattern again.

Majority Element

Longest Palindromic Substring